﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Lib.Paging;
namespace SDT.Admin.Controllers
{
    public class AjaxDataController : Controller
    {
        SDTDataContext eDataContext = new SDTDataContext();
        // Comment out by Takamaru Nguyen
        // Date: 20/06/2011
        //
        // GET: /AjaxData/Departments/1
        //[Authorize()]
        //public ActionResult Departments(int id)
        //{
        //    try
        //    {
        //        var Organization = eDataContext.Organizations.Single(e => e.ID == id);
        //        return View(Organization.ChildOrganizations.ToList());
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}        
        //[Authorize()]
        //public JsonResult SearchStaff()
        //{
        //    try
        //    {
        //        var isAdmin = UserManager.CheckRole(RoleSystem.Administrator);
        //        int inOrganizationID = UserProfile.GetOrganizationID();
        //        eDataContext.DeferredLoadingEnabled = false;
        //        var term = Request["keyword"].ToString();
        //        var keywords = eDataContext.Staffs
        //            .Where(e => e.Name.IndexOf(term) > -1 && (isAdmin ? 1 == 1 : e.Organization.ParentID == inOrganizationID))
        //            .Take(10)
        //            .ToList();
        //        return Json(keywords, JsonRequestBehavior.AllowGet);
        //    }
        //    catch
        //    {
        //        return Json("", JsonRequestBehavior.AllowGet);
        //    }
        //}        

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 20/06/2011
        /// </summary>
        //[HttpPost]
        //public ActionResult LoadDistrictByCity(int id)
        //{
        //    try
        //    {
        //        var dic = eDataContext.Districts.Where(d => d.CityID == id).ToList();
        //        return View("ucLoadDistrictByCity", dic);
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.StatusCode = 404;
        //        Response.Write(ex.Message);
        //        throw ex;
        //    }
        //}
        
        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 21/06/2011
        /// Description: The change PubYN product category.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateProductCategoryPubYN(short id, bool pubYN)
        {
            try
            {
                ProductCategory proCat = eDataContext.ProductCategories.SingleOrDefault(c => c.ID == id);
                proCat.ActiveYN = !pubYN;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 21/06/2011
        /// Description: The change PubYN product.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateProductPubYN(short id, bool pubYN)
        {
            try
            {
                Product pro = eDataContext.Products.SingleOrDefault(p => p.ID == id);
                pro.PubYN = !pubYN;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }
        /*
        [HttpPost]
        public void UpdateProductDealYN(int id, bool pubYN)
        {
            try
            {
                Product pro = eDataContext.Products.SingleOrDefault(p => p.ID == id);
                pro.DealYN = !pubYN;
                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }
		/// Author: Binhlv
		/// Date: 20/07/2011
		/// Description: The change PubYN Comments.
		[HttpPost]
		public void UpdateCommentPubYN(short id, bool pubYN)
		{
			try
			{
				Comment com = eDataContext.Comments.SingleOrDefault(p => p.ID == id);
				com.PublishYN = !pubYN;

				eDataContext.SubmitChanges();

				return;
			}
			catch (Exception ex)
			{
				Response.StatusCode = 404;
				Response.Write(ex.Message);
			}
		}
		/// Author: Binhlv
		/// Date: 20/07/2011
		/// Description: The change PubYN Company.
		[HttpPost]
		public void UpdateActivityYNCompany(short id, bool pubYN)
		{
			try
			{
				Company com = eDataContext.Companies.SingleOrDefault(p => p.ID == id);
				com.ActiveYN = !pubYN;

				eDataContext.SubmitChanges();

				return;
			}
			catch (Exception ex)
			{
				Response.StatusCode = 404;
				Response.Write(ex.Message);
			}
		}

		

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 21/06/2011
        /// Description: The change PubYN product.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateProductHotYN(int id, bool pubYN)
        {
            try
            {
                Product pro = eDataContext.Products.SingleOrDefault(p => p.ID == id);
                pro.HotYN = !pubYN;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        
        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 25/06/2011
        /// Description: The change PubYN question category.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateQuestionCategoryPubYN(short id, bool pubYN)
        {
            try
            {
                QuestionCategory proCat = eDataContext.QuestionCategories.SingleOrDefault(c => c.ID == id);
                proCat.ActiveYN = !pubYN;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }
        
        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 27/06/2011
        /// Description: The change PubYN answer
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateAnswerPubYN(short id, bool pubYN)
        {
            try
            {
                Answer proCat = eDataContext.Answers.SingleOrDefault(c => c.ID == id);
                Question que = eDataContext.Questions.Where(q=>q.ID == proCat.QuestionID).FirstOrDefault();
                
                //update status answer
                proCat.PubYN = !pubYN;

                // increment or descrement npubanswers
                if (!pubYN)
                    que.nPubAnswers = que.nPubAnswers + 1;
                else
                    que.nPubAnswers = que.nPubAnswers - 1;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 27/06/2011
        /// Description: The change PubYN answer
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateQuestionPubYN(short id, bool pubYN)
        {
            try
            {
                Question ques = eDataContext.Questions.SingleOrDefault(c => c.ID == id);
                ques.PubYN = !pubYN;
                
                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }
        
        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 27/06/2011
        /// Description: The change PubYN answer
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateQuestionCheckYN(short id, bool pubYN)
        {
            try
            {
                Question ques = eDataContext.Questions.SingleOrDefault(c => c.ID == id);
                ques.CheckYN = !pubYN;
                ques.CheckDate = DateTime.Now;
                ques.CheckBy = UserManager.GetUserId();

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }
        
        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 29/06/2011
        /// Description: The change PubYN video category
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateVideoCategoryPubYN(short id, bool pubYN)
        {
            try
            {
                VideoCategory v = eDataContext.VideoCategories.SingleOrDefault(c => c.ID == id);
                v.ActiveYN = !pubYN;
               
                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 29/06/2011
        /// Description: The change PubYN video category
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateVideoPubYN(short id, bool pubYN)
        {
            try
            {
                Video v = eDataContext.Videos.SingleOrDefault(c => c.ID == id);
                v.PubYN = !pubYN;
               
                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 29/06/2011
        /// Description: The change PubYN video category
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateVideoHomeYN(short id, bool pubYN)
        {
            try
            {
                Video v = eDataContext.Videos.SingleOrDefault(c => c.ID == id);
                v.HomeYN = !pubYN;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        
        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 14/07/2011
        /// Description: The change active article category
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateArticleCategoryPubYN(int id, bool pubYN)
        {
            try
            {
                ArticleCategory dCat = eDataContext.ArticleCategories.Where(c => c.ID == id).FirstOrDefault();
                dCat.ActiveYN = !pubYN;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 14/07/2011
        /// Description: The change PubYN article
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateArticlePubYN(int id, bool pubYN)
        {
            try
            {
                Article dCat = eDataContext.Articles.Where(c => c.ID == id).FirstOrDefault();
                dCat.PubYN = !pubYN;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }
        /// <summary>
        /// Created by Vietlv
        /// Created Date: 21/07/2011
        /// Description: The change status PubYN of table Event 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pubYN"></param>
        [HttpPost]
        public void UpdateEventPubYN(int id, bool pubYN)
        {
            try
            {
                Event ev = eDataContext.Events.Where(e => e.ID == id).FirstOrDefault();
                ev.PubYN = !pubYN;
                eDataContext.SubmitChanges();
                return;
            }
            catch (Exception exp)
            {
                Response.StatusCode = 404;
                Response.Write(exp.Message);
            }
        }
        /// <summary>
        /// Created by: Vietlv
        /// Created date: 21/07/2011
        /// Description: Change status publish of table Banner
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pubYN"></param>
        [HttpPost]
        public void UdateBannerPubYN(int id, bool pubYN)
        {
            try
            {
                Banner banner = eDataContext.Banners.Where(e => e.ID == id).FirstOrDefault();
                banner.PubYN = !pubYN;
                eDataContext.SubmitChanges();
            }
            catch (Exception exp)
            {
                Response.StatusCode = 404;
                Response.Write(exp.Message);
            }
        }
        /// <summary>
        /// Created by: Vietlv
        /// Created date: 25/07/2011;
        /// Description: Change active status of table company
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pubYN"></param>
        public void UpdateCompanyActiveYN(int id, bool pubYN)
        {
            try
            {
                Company company = eDataContext.Companies.Where(e => e.ID == id).FirstOrDefault();
                company.ActiveYN = !pubYN;
                eDataContext.SubmitChanges();
            }
            catch (Exception exp)
            {
                Response.StatusCode = 404;
                Response.Write(exp.Message);
            }
        }
        /// <summary>
        /// Created by: Vietlv
        /// Created date: 25/07/2011;
        /// Description: Change Hot status of table company
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pubYN"></param>
        public void UpdateCompanyHotYN(int id, bool pubYN)
        {
            try
            {
                Company company = eDataContext.Companies.Where(e => e.ID == id).FirstOrDefault();
                company.HotYN = !pubYN;
                eDataContext.SubmitChanges();
            }
            catch (Exception exp)
            {
                Response.StatusCode = 404;
                Response.Write(exp.Message);
            }
        }
        /// <summary>
        /// Created by: Vietlv
        /// Created date: 04/08/2011
        /// Descripton: Get ID, keyword form Tag
        /// </summary>
        /// <returns></returns>
        public JsonResult SearchTag()
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                var term = Request["keyword"].ToString();
                var keywords = eDataContext.Tags
                    .Where(e => e.Keyword.IndexOf(term) > -1)
                    .Take(10)
                    .ToList();
                return Json(keywords, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        //end
        // GET: /AjaxData/Distric/1
        [Authorize()]
        public ActionResult DistrictShare(int id)
        {
            try
            {
                var distric = eDataContext.Districts.Where(e => e.CityID == id).ToList();
                return View(distric);
            }
            catch
            {
                return View();
            }
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 14/07/2011
        /// Description: The change HotYN article
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public void UpdateArticleHotYN(int id, bool pubYN)
        {
            try
            {
                Article dCat = eDataContext.Articles.Where(c => c.ID == id).FirstOrDefault();
                dCat.HotYN = !pubYN;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        [Authorize()]
        public ActionResult GetMenus(int id)
        {
            try
            {
                var v = (from n in eDataContext.Menus where (n.ParentID == id) select n).ToList();
                return View(v.ToList());
            }
            catch
            {
                return View();
            }
        }
        [Authorize()]
        public ActionResult GetMenusByType(int id)
        {
            try
            {
                var v = (from n in eDataContext.Menus where (n.TypeID == id && n.ParentID == null) select n).ToList();
                return View(v.ToList());
            }
            catch
            {
                return View();
            }
        }
        [Authorize()]
        public ActionResult GetMenusLink(int id)
        {
            try
            {
                var m = eDataContext.Menus.Where(e => e.ID == id).SingleOrDefault();
                var typeid = Utils.ParseInt(Request["typeid"]);
                if (m.TypeID == MenuListType.Top)
                {
                    var v = (from n in eDataContext.Menus where (n.TypeID == typeid && n.ParentID == null) select n).ToList();
                    return View(v.ToList());
                }
                return View(new List<Menu>());
            }
            catch
            {
                return View();
            }
        }
        public JsonResult ValidateEmail()
        {
            try
            {
                var email = Request["Customer.Email"];
                return Json(!CustomerManager.CheckByEmail(email), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);

            }
        }
        public JsonResult ValidateUserName()
        {
            try
            {
                var userName = Request["Customer.UserName"];
                return Json(!CustomerManager.CheckUserName(userName), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);

            }
        }
        public ActionResult GetDistrict(int id)
        {
            try
            {
                var dic = eDataContext.Districts.Where(d => d.CityID == id).ToList();
                return View(dic);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
         */ 
    }
}
